package com.huluobo.a2307douyindemo.adapter

import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2307douyindemo.R
import com.huluobo.a2307douyindemo.utils.ImageUtils
import com.huluobo.a2307douyindemo.video.VideoFragment
import com.huluobo.a2307douyindemo.video.VideoViewModel
import com.huluobo.lib_base.bean.CommentBean
import org.w3c.dom.Text

/**
 *  Created by LC on 2025/9/16.
 */
class CommentAdapter(
    private val viewModel: VideoViewModel,
    private val videoId: Int,
    private val fragment: VideoFragment
) :
    BaseQuickAdapter<CommentBean, BaseViewHolder>(R.layout.item_comment) {
    private val childCommentAdapter: ChildCommentAdapter = ChildCommentAdapter()

    override fun convert(holder: BaseViewHolder, item: CommentBean) {
        viewModel.getChildCommentSuccess.observe(fragment) {
            childCommentAdapter.data.clear()
            childCommentAdapter.data.addAll(it)
            childCommentAdapter.notifyDataSetChanged()
        }
        if (!item.icon.isNullOrEmpty()) {
            ImageUtils.loadImage(context, item.icon, holder.getView(R.id.item_comment_icon_iv))
        }
        holder.setText(R.id.item_comment_name_tv, item.username)
        holder.setText(R.id.item_comment_msg_tv, item.msg)
        holder.setText(R.id.item_comment_time_tv, item.createtime)
        //判断是否有二级评论,如果有,展示更多布局,没有不展示
        if (item.childcount > 0) {
            holder.getView<TextView>(R.id.item_comment_more_tv).visibility = View.VISIBLE
        } else {
            holder.getView<TextView>(R.id.item_comment_more_tv).visibility = View.GONE
        }

        //初始化二级评论列表
        holder.getView<RecyclerView>(R.id.item_comment_more_rv).layoutManager = LinearLayoutManager(context)
        holder.getView<RecyclerView>(R.id.item_comment_more_rv).adapter = childCommentAdapter

        //二级评论列表的点击事件
        holder.getView<TextView>(R.id.item_comment_more_tv).setOnClickListener {
            //发起请求
            viewModel.getChildComment(1, 10, item.rootid, videoId)
            //隐藏加载的textView
            holder.getView<TextView>(R.id.item_comment_more_tv).visibility = View.GONE
            //显示二级评论布局
            holder.getView<RecyclerView>(R.id.item_comment_more_rv).visibility = View.VISIBLE
        }
    }
}